package com.wf.course.service.impl;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.utils.page.PageBean;
import com.utils.security.EncryptionHelper;
import com.utils.security.UUIDGenerator;
import com.wf.course.dao.ICoinLogMapper;
import com.wf.course.dao.IGiveCoinLogMapper;
import com.wf.course.dao.IMemberGoldLogMapper;
import com.wf.course.dao.IMemberMapper;
import com.wf.course.model.member.CoinLog;
import com.wf.course.model.member.GiveCoinLog;
import com.wf.course.model.member.Member;
import com.wf.course.model.member.MemberGoldLog;
import com.wf.course.service.IMemberService;

@Service
@Transactional(rollbackFor=Exception.class)
public class IMemberServiceImpl implements IMemberService{
	
	@Autowired
	private IMemberMapper mapper;
	@Autowired
	private ICoinLogMapper smapper;
	@Autowired
	private IGiveCoinLogMapper gmapper;
	@Autowired
	private IMemberGoldLogMapper goldmapper;
	@Override
	public void indexInfo(PageBean<Member> pageBean) {
		// TODO Auto-generated method stub
		List<Member> lists =this.mapper.queryList(pageBean);
		
		for(Member gors : lists){
			int userId = gors.getUserId();
			//计算原铜钱
			List<CoinLog> listName=this.smapper.getUserid(userId);
			String coinValue = "";
	         if(listName!=null &&listName.size()>0 &&listName.get(0)!=null){
				
	        	 coinValue= listName.get(0).getCoinValue();
				
			}
	       //计算新铜钱
				List<GiveCoinLog> listNames=this.gmapper.getUserid(userId);
				String newcoinValue = "";
		         if(listNames!=null &&listNames.size()>0 &&listNames.get(0)!=null){
					
		        	 newcoinValue= listNames.get(0).getCoinValue();
					
				}
		       //计算金币
					List<MemberGoldLog> golds=this.goldmapper.getUserid(userId);
					String gold = "";
			         if(golds!=null &&golds.size()>0 &&golds.get(0)!=null){

			        	 gold= golds.get(0).getGoldValue();

					}

			// 新金币
			List<MemberGoldLog> newGolds = this.goldmapper.getNewGoldLogByUserId(userId);
			String newGold = "";
			if (newGolds != null && newGolds.size() > 0 && newGolds.get(0) != null) {
				newGold = newGolds.get(0).getGoldValue();
			}
			      
			         BigDecimal gold1=null;
			         BigDecimal newGold1=null;
			         BigDecimal newcoinValue1=null;
			         BigDecimal coinValue1=null;
			         BigDecimal balance =null;
			         if(StringUtils.isNotBlank(gold)){
			        	 gold1 = new BigDecimal(gold);
			         }
					 if(StringUtils.isNotBlank(newGold)){
						 newGold1 = new BigDecimal(newGold);
					 }
			         if(StringUtils.isNotBlank(newcoinValue)){
			          newcoinValue1 = new BigDecimal(newcoinValue);
			         }
			         if(StringUtils.isNotBlank(coinValue)){
			        	coinValue1 = new BigDecimal(coinValue);
			         }
			         
			 gors.setGold(gold);
			 gors.setNewGold(newGold);
		     gors.setNewcoinValue(newcoinValue);
	         gors.setCoinValue(coinValue);
	         if(gold1!=null&&newcoinValue1!=null&&coinValue1!=null){
	         balance =gold1.add(newcoinValue1).add(coinValue1);
	         }else if(gold1!=null&&newcoinValue1!=null&&coinValue1==null){
	        	 balance =gold1.add(newcoinValue1);
	         }else if(gold1!=null&&newcoinValue1==null&&coinValue1!=null){
	        	 balance =gold1.add(coinValue1);
	         }else if(gold1==null&&newcoinValue1!=null&&coinValue1!=null){
	        	 balance =newcoinValue1.add(coinValue1);
	        	 
	         }
	         if (newGold1 != null) {
	         	balance = balance.add(newGold1);
			 }
	         gors.setBalance(balance);
	         
		}
		
		pageBean.setResults(lists);
		pageBean.setTotalCount(this.mapper.getTotalCount(pageBean).intValue());
	}

	@Override
	public void saveOrUpdateMember(Member member) {
		// TODO Auto-generated method stub
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		member.setUpdateTime(time);
		if(member.getUserId()==null){
			String salt = UUIDGenerator.getUUID(false);//随机字符串
			member.setUserUuidSalt(salt);
			String password = EncryptionHelper.encrypt(member.getUserPassword(), salt);//密码
			member.setUserPassword(password);			
			member.setCreateTime(time);//时间
			this.mapper.saveMember(member);
		}else{
			this.mapper.updateMember(member);
		}
		
	}

	@Override
	public Member queryMemberById(Integer id) {
		// TODO Auto-generated method stub
		return this.mapper.queryMemberById(id);
	}

	@Override
	public void deleteById(Integer id) {
		// TODO Auto-generated method stub
		this.mapper.deleteById(id);
	}

	@Override
	public List<Member> queryMembers(){
		return this.mapper.queryMembers();
	}
	@Override
	public List<Member> queryCustoms(){
		return this.mapper.queryMembers();
	}

	@Override
	public List<Member> queryMembersByTelAndId(String telephone,Integer userId) {
		// TODO Auto-generated method stub
		return this.mapper.queryMembersByTelAndId(telephone,userId);
	}

	@Override
	public List<Member> queryMembersByTelephone(String telephone) {
		// TODO Auto-generated method stub
		return this.mapper.queryMembersByTelephone(telephone);
	}

	@Override
	public void queryAllowGiftMember(PageBean<Member> pageBean) {
		// TODO Auto-generated method stub
		pageBean.setResults(mapper.queryAllowGiftMember(pageBean));
		pageBean.setTotalCount(mapper.queryAllowGiftMemberCount(pageBean));
	}
	

	@Override
	public Integer queryAllowGiftMemberCount(PageBean<Member> pageBean) {
		// TODO Auto-generated method stub
		return mapper.queryAllowGiftMemberCount(pageBean);
	}

	@Override
	public List<String> getAllMemberUserId(PageBean<Member> pageBean) {
		// TODO Auto-generated method stub
		return mapper.getAllMemberUserId(pageBean);
	}
	@Override
	public List<String> getAllMemberUserIdByRightJoin(PageBean<Member> pageBean) {
		// TODO Auto-generated method stub
		return mapper.getAllMemberUserIdByRightJoin(pageBean);
	}

}
